package com.xbq.exceleditor.ui.filechooser

import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.xbq.exceleditor.R
import com.xbq.exceleditor.databinding.ItemFileBinding
import com.xbq.xbqsdk.util.format.FormatUtils
import java.io.File
import javax.inject.Inject

/**
 * Author: liaohaiping
 * Time: 2018-07-25
 * Description:
 */
class FileAdapter @Inject constructor() :BaseQuickAdapter<File,BaseViewHolder>(R.layout.item_file) {
    private var selectedFile: File?=null
    fun selectFile(file:File){
        selectedFile?.let { notifyFileChanged(it) }
        selectedFile =file
        notifyFileChanged(file)
    }

    fun notifyFileChanged(file:File){
        val index = data.indexOf(file)
        if(index>=0){
            notifyItemChanged(index)
        }else{
            notifyDataSetChanged()
        }
    }

    override fun convert(
        holder: BaseViewHolder,
        item: File
    ) {
        ItemFileBinding.bind(holder.itemView).let {
            if(item.isDirectory) {
                it.ivImage.setImageResource(R.drawable.ic_folder_close)
                it.tvFileSize.visibility=View.GONE
            }else{
                it.ivImage.setImageResource(R.drawable.ic_f_excel)
                it.tvFileSize.visibility=View.VISIBLE
                it.tvFileSize.setText(FormatUtils.formatFileSize(item.length()))
            }
            it.tvFileName.setText(item.getName())
            if(item==selectedFile){
                it.ivCheck.visibility= View.VISIBLE
            }else{
                it.ivCheck.visibility= View.GONE
            }
        }
    }
}